from manimlib import *
from numpy import half, insert

LV,QL,QH,JH='#75cfb8','#bbdfc8','#f0e5d8','#ffc478'

class p1(Scene):
    def construct(self):
        # self.add(NumberPlane())i889
        a,b=[-1.92,2.31,0],[2.92,0.69,0]
        p,t,p_=ORIGIN,[1,0,0],[1,3,0]
        m,n=[-3,0,0],[3,0,0]
        #
        ab=Line(a,b)
        #
        c3=Arc(3,angle=PI).set_fill(LV,1)
        insert=Intersection(Circle(3),Polygon(a,b,[3,3,0],[-3,3,0])).set_fill(LV,1)
        self.add(c3,Line(LEFT*3,RIGHT*3))
        self.add(
            ab,
            insert.copy().set_fill(WHITE,1),
            insert
        )
        self.play(Rotate(insert,PI,axis=np.array(b)-np.array(a),about_point=np.array(b)))
        b4,b2=Brace(Line(m,t)),Brace(Line(t,n))
        self.add(b4,b2,
            Tex('4').next_to(b4,DOWN),
            Tex('2').next_to(b2,DOWN),
            Text('折痕长度等于多少 ?').shift(DOWN*2)
        )
        self.wait()

class p2(Scene):
    def construct(self):
        # self.add(NumberPlane())
        self.camera.frame.save_state()
        self.camera.frame.move_to([0,3,0])
        a,b=[-1.92,2.31,0],[2.92,0.69,0]
        p,t,p_=ORIGIN,[1,0,0],[1,3,0]
        m,n=[-3,0,0],[3,0,0]
        #
        ab=Line(a,b)
        #
        arc_3=Arc(3,angle=PI).set_fill(LV,1)
        insert=Intersection(Circle(3),Polygon(a,b,[3,3,0],[-3,3,0])).set_fill(LV,1)
        self.add(arc_3,Line(LEFT*3,RIGHT*3))
        self.add(
            ab,
            insert.copy().set_fill(WHITE,1),
            insert
        )
        # p2
        self.add(insert.rotate(PI,axis=np.array(b)-np.array(a),about_point=np.array(b)))
        c3=Circle(3).move_arc_center_to(p_)
        self.add(c3,Dot(p),Dot(t),Dot(p_),
            Tex('A').next_to(a,UL),
            Tex('B').next_to(b),
            Tex('P').next_to(p,DOWN),
            Tex('T').next_to(t,DOWN)
        )
        pp_,tp_=Line(p,p_),Line(t,p_)
        c=line_intersection(ab.get_start_and_end(),pp_.get_start_and_end())
        self.add(pp_,tp_,
            Tex('P^\prime').next_to(p_),
            Tex('C').next_to(c,UL).shift(RIGHT*0.2),
            Angle(Line(c,a),Line(c,p),elbow=True),
            Angle(tp_,Line(t,p),elbow=True),
            Line(p,a)
        )
        self.wait()

class p3(Scene):
    def construct(self):
        # self.add(NumberPlane())
        # self.camera.frame.save_state()
        # self.camera.frame.move_to([0,3,0])
        side=6
        abcd=Square(side)
        half_side=side/2
        a,b,c,d=[-half_side,half_side,0],[half_side,half_side,0],[half_side,-half_side,0],[-half_side,-half_side,0]
        self.add(abcd)
        #
        ad,bc=Line(a,d),Line(b,c)
        e,f=bc.point_from_proportion(3/8),ad.point_from_proportion(0.875)
        ef=Line(e,f)
        polygon=Polygon(e,f,d,c).rotate(PI,np.array(f)-np.array(e),e)
        c_=polygon.get_boundary_point(UP)
        self.add(ef,polygon,
            Tex('A').next_to(a,UL),
            Tex('B').next_to(b,UR),
            Tex('C').next_to(c,DR),
            Tex('D').next_to(d,DL),
            Tex('E').next_to(e),
            Tex('F').next_to(f,DR),
            Tex('G').next_to(f).shift(UP),
            Tex('C^\prime').next_to(c_,UP),
            Tex('D^\prime').next_to(polygon,LEFT).shift(DOWN*2),
            Line(c,c_)
        )
        self.wait()